// Copyright DustedPixels.com 2008. All rights reserved.

package com.dustedpixels.jasmin.chips;

/**
 * @author micapolos@gmail.com (Michal Pociecha-Los)
 */
public final class Memory3 implements Chip {
  public final IntBus2 ADDRESS = new IntBus2();
  public final ByteBus2 DATA = new ByteBus2();
  public final Pin2 WR = new Pin2();
  public final Pin2 REQ = new Pin2();
  
  private final int mask;
  final byte[] mem;
  
  public Memory3(int addressBits) {
    this.mask = (1 << addressBits) - 1;
    this.mem = new byte[1 << addressBits];
  }
  
  public void update() {
    DATA.reset();
    if (REQ.get()) {
      if (WR.get()) {
        mem[ADDRESS.get() & mask] = DATA.get();
      } else {
        DATA.set(mem[ADDRESS.get() & mask]);
      }
    }
  }
}
